package com.example.demo.controller;

import com.example.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
public class userController {
    //通过设置请求方式将请求方法设为唯一
    @RequestMapping(value = "/sayHi", method = RequestMethod.GET)
        public String sayHi() {
        return "hi";
        }
        //请求传参
        @RequestMapping("/name")
        public String name(String name) {
        return "请求到一个参数" + name;
        }
        //若此时传递age值为空，则会报错，
        //int是基本类型，默认值为0，而不是null
        @RequestMapping("/age")
        public int age(int age) {
        return age;
        }
        //传递对象
        //但此时对int类型传递为null则不会报错，默认为0
        @RequestMapping("/r3")
        public String r3(UserInfo user){
        return user.toString();
        }
        //后端进行参数改名
        @RequestMapping("/r4")
        //使用RequestParam进行传参时，该参数就默认成了必传参数，可通过设置属性将其改为非必传
        public String r4(@RequestParam(value = "name" ,required = false) String username, int age){
        return username + " " + age;
        }
        @RequestMapping("/r5")
        //传递数组
        public String r5(int[] arr){
        return Arrays.toString(arr);
        }
        //传递集合,无法将传入的参数转化为集合，此处需要加入注解,并且要注意当传入参数为空时会发生空指针异常
        @RequestMapping("/r6")
        public String r6(@RequestParam(required = false) List<Integer> list){
        if( list != null ){
            return Arrays.toString(list.toArray());
        } else {
            return "null";
        }
        }
        //从body中获取数据信息
        @RequestMapping("/r7")
        public String r7(@RequestBody UserInfo user){
        return user.toString();
        }
        //从url路径中获取信息,这里RequestMapping中的变量名称要与下面函数参数名称相同
        @RequestMapping("/r8/{name}/{age}")
        public String r8(@PathVariable("name") String userName, @PathVariable Integer age){
        return userName + " " + age;
        }
        //上传文件
        @RequestMapping("/r9")
        //RequestPart 不写也可以，当需要参数重命名时，可以用到
        public String r9(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("E:/tmp/" + fileName));
        return "接收到的文件名为：" + fileName;
        }
        //获取cookie，该方法可获取到所有cookie
        @RequestMapping("/r10")
        public String r10(HttpServletRequest request, HttpServletResponse response) {
            Cookie[] cookies = request.getCookies();
            if(cookies != null){
                for(Cookie cookie : cookies){
                    System.out.println(cookie.getName() + cookie.getValue());
                }
                return "cookie获取成功";
            }
            return "null";
        }
        //通过SpringBoot方式获取，只可获取到指定名称，不可获取全部
        @RequestMapping("/r11")
        public String r11(@CookieValue("xxwu") String xxwu) {
        return xxwu + "xxwu";
        }
        //session的获取
        @RequestMapping("/setSession")
        public String setSession(HttpServletRequest request) {
            HttpSession session = request.getSession();
            session.setAttribute("uerName", "zhangsan");
            return "设置session成功";
        }
        @RequestMapping("/getSession")
        public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession();//如果session为空会自动创建一个session对象
        return (String)session.getAttribute("uerName");
        }
        @RequestMapping("/getSession2")
        //使用SpringBoot方式获取，这里的函数参数就已经实现了上面getSession方法的第一行
        public String getSession2(HttpSession session) {
        return (String)session.getAttribute("uerName");
        }
        //第三种写法，更加简洁,将SessionAttribute的userName赋值给String userName
        //但这种写法在session为空时会报错,可通过参数修改设置为非必传
        @RequestMapping("/getSession3")
        public String getSession3(@SessionAttribute("useName") String useName) {
        return useName;
        }
        //获取Header
        //传统方式
        @RequestMapping("/getHeader")
        public String getHeader(HttpServletRequest request) {
        //这里的User-Agent是Header中键值对的key，通过key获取value
        String userAgent = request.getHeader("User-Agent");
        return userAgent;
        }
        //通过SpringBoot方式，通过注解省略一句代码
        @RequestMapping("/getHeader2")
        public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return userAgent;
        }

}
